package com.lagou.edu.utils;

import java.sql.SQLException;

/**
 * @author 赵乾坤
 * @since 2021-12-04 14:10
 * 事务管理器类：负责手动事务的开启、提交、回滚
 * 基于面向对象思想，把方法封装为对象
 */
public class TransactionManager {

    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }
/*
    *//**
     * 关闭构造器（私有化）单列模式饿汉式
     *//*
    private TransactionManager() {

    }
    private static TransactionManager transactionManager;

    public static TransactionManager getInstance() {
        return transactionManager;
    }*/


    /**
     *  开启手动事务控制
     * @throws SQLException SQLException
     */
    public void beginTransaction() throws SQLException {
        // 关闭自动提交
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    /**
     * 提交事务
     * @throws SQLException SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }


    /**
     * 回滚事务
     * @throws SQLException SQLException
     */
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
